package struts.action.etudiant;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import metier.Candidature;
import metier.Etudiant;
import metier.Stage;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import dao.DAOCandidature;
import dao.DAOEntreprise;
import dao.DAOStage;

public class ActionListeCandidatureEtu extends Action {
	
	private DAOStage daoStage=null;
	private DAOCandidature daoCandidature=null;
	private DAOEntreprise daoEntreprise=null;
	
	public DAOStage getDaoStage() {
		return daoStage;
	}
	
	public void setDaoStage(DAOStage daoStage) {
		this.daoStage = daoStage;
	}
	
	public DAOCandidature getDaoCandidature() {
		return daoCandidature;
	}
	
	public void setDaoCandidature(DAOCandidature daoCandidature) {
		this.daoCandidature = daoCandidature;
	}
	
	public DAOEntreprise getDaoEntreprise() {
		return daoEntreprise;
	}

	public void setDaoEntreprise(DAOEntreprise daoEntreprise) {
		this.daoEntreprise = daoEntreprise;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception
	{		
		HttpSession session = request.getSession(false);
		
		Etudiant etud = (Etudiant) session.getAttribute("etudiant");
		
		if(etud == null)
			return mapping.findForward("fatalError");
		
		ArrayList<Stage> stages = new ArrayList<Stage>();
		ArrayList<String> entreprises = new ArrayList<String>();
		ArrayList<Candidature> candidatures = getDaoCandidature().loadAll(etud.getNumEtudiant());		
		
		if(candidatures.size() == 0)
		{
			request.setAttribute("NOTHING", "Vous n'avez aucune candidature.");
			return mapping.findForward("affichCandidaturesEtu");
		}
		
		for(Candidature c : candidatures)
		{
			Stage sta = getDaoStage().getStage(c.getNumStage());
			stages.add(sta);
			entreprises.add(getDaoEntreprise().get(sta.getReferenceEntreprise()).getNom());
			
		}
		
		request.setAttribute("STAGES", stages);
		request.setAttribute("ENTREPRISES", entreprises);
		request.setAttribute("CANDIDATURES", candidatures);
		
		return mapping.findForward("affichCandidaturesEtu");
	}
}
